package models

import (
	"gorm.io/gorm"
)

type Register struct {
	gorm.Model
	Number  string `json:"number" gorm:"type:varchar(30);not null;comment:就诊卡号'"`
	Name    string `json:"name" gorm:"type:varchar(30);not null;comment:患者姓名"`
	Tel     string `json:"tel" gorm:"type:char(11);not null;comment:手机号码"`
	Uid     string `json:"uid" gorm:"type:char(18);not null;comment:证件号"`
	Address string `json:"address" gorm:"type:varchar(30)not null;comment:就诊地址"`
}

func (r *Register) Creates() error {
	return DB.Create(&r).Error
}

func (r *Register) List(page, pageSize int) (register []Register, err error) {
	err = DB.Scopes(Paginate(page, pageSize)).Find(&register).Error
	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
